package jvmPractice.cap2;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * 方法区和运行时常量池溢出
 * 代码清单2-9　借助CGLib使得方法区出现内存溢出异常
 * VM Args：-XX:PermSize=10M -XX:MaxPermSize=10M
 * JDK 8以后，永久代便完全退出了历史舞台，元空间作为其替代者登场
 * -XX：MaxMetaspaceSize=5M -XX：MetaspaceSize=5M
 * @author zzm
 */
public class JavaMethodAreaOOM {
    public static void main(String[] args) {
        while (true) {
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(OOMObject.class);
            enhancer.setUseCache(false);
            enhancer.setCallback(new MethodInterceptor() {
                @Override
                public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                    return methodProxy.invoke(o, objects);
                }
            });
        }
    }
    static class OOMObject {
    }
}
